import { createActions } from 'redux-actions'

const init = (config) => {
  const actionCreators = createActions({
    'chooseCreateGame': gameData => gameData,
    'ruleConfClicked': data => data,
    'changeCurrentGroup': data => data,
    'currentGroupRules': data => data,
    'currentSelectedGroupRule': data => data,
    'openGroupModal': modalContent => modalContent,
    'closeGroupModal': data => data,
    'currentTableData': data => data
  })
  const queryCurrentGroupRules = (dispatch, getState) => {
    const {app, group, config} = getState()
    const {currentGroup} = group
    const gameList = app.gameData
    if (!currentGroup) {
      return
    }
    var uid = app.userInfo.uid
    if (uid) {
      localStorage.setItem('userInfo', JSON.stringify(app.userInfo))
    } else {
      uid = JSON.parse(localStorage.getItem('userInfo')).uid
    }
    const crowdId = currentGroup.id
    const url = `${global.config.pre}/crowd/room/info/${Cookies.get('areaCode')}/${crowdId}/${uid}`
    const opt = {method: 'GET'}
    fetchJson(url, opt).then(currentData => {
      console.log('currentData', currentData);
      if (currentData.result) {
        dispatch(actionCreators.currentGroupRules([]))
        if (currentData.result == 600) {
          window.location.href = currentData.data //
          sessionStorage.setItem('result600', 'true')
      }
        return
      }
      dispatch(actionCreators.currentTableData(currentData.data.cacheList))
      const state = getState()
      const {currentGroup} = state.group
      const gameList = state.app.gameData
      if (!currentGroup) {
        return
      }
      const url = `${global.config.pre}/crowd/conf/${Cookies.get('areaCode')}`
      const opt = {method: 'GET'}
      const query = {
        crowdId: currentGroup.id,
        offset: 0,
        limit: 10
      }
      fetchJson(url, opt, query).then(data => {
        console.log(data);
        if (data.result) {
          return
        }
        var currentSelect
        data.data.dataList.forEach(element => {
          if (element.id === currentData.data.conf.id) {
            currentSelect = element
          }
          gameList.some(gameData => {
            if (gameData.gameId === element.gameType) {
              element.gameConf = gameData
            }
            return gameData.gameId === element.gameType
          })
        })
        dispatch(actionCreators.currentGroupRules(data.data.dataList))
        if (currentSelect) {
          dispatch(actionCreators.currentSelectedGroupRule(currentSelect))
        }
      })
    })
  }
  const queryCurrentGroupRulesNoRoom = (dispatch, getState) => {
    const {app, group, config} = getState()
    const {currentGroup} = group
    const {gameList} = config
    if (!currentGroup) {
      return
    }
    var uid = app.userInfo.uid
    if (uid) {
      localStorage.setItem('userInfo', JSON.stringify(app.userInfo))
    } else {
      uid = JSON.parse(localStorage.getItem('userInfo')).uid
    }
    const crowdId = currentGroup.id
    const url = `${global.config.pre}/crowd/room/info/${Cookies.get('areaCode')}/${crowdId}/${uid}`
    const opt = {method: 'GET'}
    fetchJson(url, opt).then(currentData => {
      console.log(currentData);
      if (currentData.result) {
        dispatch(actionCreators.currentGroupRules([]))
        if (currentData.result == 600) {
          console.log('result == 600')
          sessionStorage.setItem('result600', 'true')
          window.location.href = currentData.data //
        }
        return
      }
      const state = getState()
      const {currentGroup} = state.group
      const {gameList} = state.config
      if (!currentGroup) {
        return
      }
      const url = `${global.config.pre}/crowd/conf/${Cookies.get('areaCode')}`
      const opt = {method: 'GET'}
      const query = {
        crowdId: currentGroup.id,
        offset: 0,
        limit: 10
      }
      fetchJson(url, opt, query).then(data => {
        console.log(data);
        if (data.result) {
          return
        }
        data.data.dataList.forEach(element => {
          gameList.some(gameData => {
            if (gameData.gameId === element.gameType) {
              element.gameConf = gameData
            }
            return gameData.gameId === element.gameType
          })
        })
        dispatch(actionCreators.currentGroupRules(data.data.dataList))
      })
    })
  }
  // 改变默认的玩法
  const currentSelectedGroupRule = pointConf => (dispatch, getState) => {
    const {app, group, config} = getState()
    const {currentGroup} = group
    if (!currentGroup) {
      return
    }
    const uid = app.userInfo.uid
    const crowdId = currentGroup.id
    const url = `${global.config.pre}/crowd/room/${Cookies.get('areaCode')}`
    const opt = {
      method: 'PUT',
      body: JSON.stringify({crowdId, uid, pointConfId: pointConf.id})
    }
    console.log('opt', opt);
    fetchJson(url, opt).then(currentData => {
      console.log(currentData);
      if (currentData.result) {
        return
      }
      dispatch(queryCurrentGroupRules)
    })
  }
  const group = {
    chooseCreateGame: actionCreators.chooseCreateGame,
    ruleConfClicked: actionCreators.ruleConfClicked,
    changeCurrentGroup: actionCreators.changeCurrentGroup,
    createGame: (params) => (dispatch, getState) => {
      console.error(params)
      const {alias, bigWinner, otherPlayers, otherPayType} = params;

      const {group} = getState()
      const {config, gameType, gameName} = group.chooseCreateGame
      
      var roomConf: any = {}
      config.options.forEach(conf => {
        if (!conf.muti) {
          roomConf[conf.key] = conf.selected[0]
        } else {
          roomConf[conf.key] = conf.selected
        }
      })

      // 自定义支付处理
      if(otherPayType) {
        roomConf.payType = 4;
        roomConf.winnerPayed = bigWinner;
        roomConf.otherPayed = otherPlayers;
      }
      
      if (gameName === '点子牛') {
        if (roomConf.ruleType === 6) {
          roomConf.scopeType = 1
        } else if (roomConf.ruleType === 7) {
          roomConf.scopeType = 2
        }
      }
      if (gameName === '斗公牛') {
        roomConf.ruleType = 0
      }
      if (gameName === '长沙麻将') {
        roomConf.liuliushun = roomConf.wanfa.some(wan => wan === 'liuliushun')
        roomConf.queyise = roomConf.wanfa.some(wan => wan === 'queyise')
        roomConf.banbanhu = roomConf.wanfa.some(wan => wan === 'banbanhu')
        roomConf.dasixi = roomConf.wanfa.some(wan => wan === 'dasixi')
        roomConf.santong = roomConf.wanfa.some(wan => wan === 'santong')
        roomConf.jiejiegao = roomConf.wanfa.some(wan => wan === 'jiejiegao')
        roomConf.yizhihua = roomConf.wanfa.some(wan => wan === 'yizhihua')
      }
      if (gameName === '平江扎鸟') {
        roomConf.pinghu = true
      }
      if (gameName === '平江开船') {
        roomConf.ruleType = 0
      }
      if (gameName === '惠州麻将') {
        roomConf.daifeng = roomConf.wanfa.some(wan => wan === 'daifeng')
        roomConf.canJiHu = roomConf.wanfa.some(wan => wan === 'canJiHu')
        roomConf.menQingJiafen = roomConf.wanfa.some(wan => wan === 'menQingJiafen')
        roomConf.genzhuan = roomConf.wanfa.some(wan => wan === 'genzhuan')
        roomConf.duoHuaHu = roomConf.wanfa.some(wan => wan === 'duoHuaHu')
        roomConf.canQiDui = roomConf.wanfa.some(wan => wan === 'canQiDui')
        roomConf.magengang = roomConf.wanfa.some(wan => wan === 'magengang')

        roomConf.magengdifen = roomConf.maMode.some(wan => wan === 'magengdifen')
        roomConf.magenduipeng = roomConf.maMode.some(wan => wan === 'magenduipeng')
        roomConf.magengang = roomConf.maMode.some(wan => wan === 'magengang')
        
      }
      if (gameName === '跑得快') {
        roomConf.gameFinal = [2, 3, 4]
      }
      if (gameName === '欢乐金花') {
        roomConf.bottomType = 1
        roomConf.compareType = roomConf.showType
        
        if(roomConf.playMethod.indexOf(1) !== -1) {
          roomConf.gameDark = 1;
        } else {
          roomConf.gameDark = 0;
        }
        if(roomConf.playMethod.indexOf(3) !== -1) {
          roomConf.autoOnOffline = true;
        } else {
          roomConf.autoOnOffline = false;
        }
        if(roomConf.playMethod.indexOf(2) !== -1) {
          roomConf.baoZiXi = true;
        } else {
          roomConf.baoZiXi = false;
        }
        if(roomConf.playMethod.indexOf(4) !== -1) {
          roomConf.shunJinXi = true;
        } else {
          roomConf.shunJinXi = false;
        }


      }
      if (gameName === '三公') {
        roomConf.maxPlayerCount = 6;
      }

      //        clientContent: JSON.stringify(group.chooseCreateGame),
      const send = {
        jsonContent: JSON.stringify(roomConf),
        gameType,
        confName: alias ? alias : name,
      }
      const url = `${global.config.pre}/crowd/conf/${Cookies.get('areaCode')}/create`
      const opt = {
        method: 'POST',
        body: JSON.stringify(send)
      }
      console.log('send', send);
      fetchJson(url, opt).then(data => {
        console.log('create club method' ,data);
        if(data.result) {
          return dispatch(actionCreators.openGroupModal(data.message))
        }
        dispatch(actionCreators.openGroupModal('创建成功'));
        window.location.hash = '#/ClubDetails'
      })
    },
    currentSelectedGroupRule,
    queryCurrentGroupRules,
    queryCurrentGroupRulesNoRoom,
    openModal: actionCreators.openGroupModal,
    closeModal: actionCreators.closeGroupModal
  }
  return group
}

export default init